package com.platform.system.service.impl;

import java.util.ArrayList;
import java.util.List;
        import com.platform.common.utils.DateUtils;
import com.platform.system.domain.ProCart;
import com.platform.system.domain.ProProduct;
import com.platform.system.mapper.ProProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.platform.system.mapper.ProProductOrderMapper;
import com.platform.system.domain.ProProductOrder;
import com.platform.system.service.IProProductOrderService;

/**
 * 商家Service业务层处理
 *
 * @author platform
 * @date 2025-02-27
 */
@Service
public class ProProductOrderServiceImpl implements IProProductOrderService {
    @Autowired
    private ProProductMapper proProductMapper;

    @Autowired
    private ProProductOrderMapper proProductOrderMapper;

    /**
     * 查询商家
     *
     * @param id 商家主键
     * @return 商家
     */
    @Override
    public ProProductOrder selectProProductOrderById(Long id) {
        return proProductOrderMapper.selectProProductOrderById(id);
    }

    /**
     * 查询商家列表
     *
     * @param proProductOrder 商家
     * @return 商家
     */
    @Override
    public List<ProProductOrder> selectProProductOrderList(ProProductOrder proProductOrder) {
        return proProductOrderMapper.selectProProductOrderList(proProductOrder);
    }

    /**
     * 新增商家
     *
     * @param proProductOrder 商家
     * @return 结果
     */
    @Override
    public int insertProProductOrder(ProProductOrder proProductOrder) {
                proProductOrder.setCreateTime(DateUtils.getNowDate());
            return proProductOrderMapper.insertProProductOrder(proProductOrder);
    }

    /**
     * 修改商家
     *
     * @param proProductOrder 商家
     * @return 结果
     */
    @Override
    public int updateProProductOrder(ProProductOrder proProductOrder) {
                proProductOrder.setUpdateTime(DateUtils.getNowDate());
        return proProductOrderMapper.updateProProductOrder(proProductOrder);
    }

    /**
     * 批量删除商家
     *
     * @param ids 需要删除的商家主键
     * @return 结果
     */
    @Override
    public int deleteProProductOrderByIds(Long[] ids) {
        return proProductOrderMapper.deleteProProductOrderByIds(ids);
    }

    /**
     * 删除商家信息
     *
     * @param id 商家主键
     * @return 结果
     */
    @Override
    public int deleteProProductOrderById(Long id) {
        return proProductOrderMapper.deleteProProductOrderById(id);
    }

    /**
     * 根据订单id查询订单关联的商品列表
     * @param orderId
     * @return
     */
    public List<ProCart> findProductByOrderId(Long orderId) {
        List<ProCart> carts = new ArrayList<>();
        List<ProProductOrder> proProductOrders = proProductOrderMapper.selectProProductOrderByOrderId(orderId);
        for (ProProductOrder product : proProductOrders) {
            Long goodsId = product.getProducproId();
            ProProduct goods = proProductMapper.selectProProductById(goodsId);
            ProCart cart = new ProCart();
            cart.setGoods(goods);
            cart.setProducproId(goodsId);
            cart.setCount(product.getCount());
            carts.add(cart);
        }
        return carts;
    }

    /**
     * 根据订单id查询订单关联的商品列表
     * @param orderId
     * @return
     */
    public Long findProductPointByOrderId(Long orderId) {
        Long points = 0L;
        List<ProProductOrder> proProductOrders = proProductOrderMapper.selectProProductOrderByOrderId(orderId);
        for (ProProductOrder product : proProductOrders) {
            ProProduct proProduct = proProductMapper.selectProProductById(product.getProducproId());
            Long resPoints = proProduct.getPoints() * product.getCount();
            points += resPoints;
        }
        return points;
    }
}
